#coding=utf8
#-------------------------------------------------------------------------------
# Name:        ??1
# Purpose:
#
# Author:      weigun
#
# Created:     11-08-2011
# Copyright:   (c) weigun 2011
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import os
import re
import Log
from multiprocessing import Process,Queue
import SvnClient
import Notify
import AppConfig
import SvnConflictChecker
class Main():
    pass

#--------------------------------------------------------

def svn_update_pro(queue_dict,conf):
    ''' 更新svn'''
    print 'svn_update_pro'
    #--queue_dict['svnconflict_queue'].put(['weigun'])
    for section in conf.get_sections():
        working_copy = conf.get_section_items(section).has_key('workingcopy')
        if conf.get_section_items(section).has_key('workingcopy'):
            working_copy = conf.get_section_items(section).get('workingcopy')
            svn = SvnClient.SvnClient( conf.get_section_items(section).get('svnurl'),conf.get_section_items(section).get('user'), conf.get_section_items(section).get('passwd'))
            svn.update(working_copy)
            print working_copy + 'was updated'
            queue_dict['log_queue'].put(working_copy + 'was updated')

def notify_pro(queue_dict ,conf):
    '''发送邮件进程 '''
    print 'notify_pro'
    notify = Notify.Notify(queue_dict,conf)
    while True:
        try:
            mail_data = queue_dict['notify_queue'].get()
            if mail_data :
                if mail_data == 'end' :
                    break
                notify.add(mail_data)
            else:
                info = 'notify_queue get() timeout,now next'
                print info
                queue_dict['log_queue'].put(info)
        except:
            queue_dict['log_queue'].put('read notify_queue error')
    notify.notify()



def check_pro(queue_dict ,conf):
    '''检查svn更新进程'''
    print 'check_pro running'
    checker = SvnConflictChecker.SvnConflictChecker(queue_dict,conf)
    checker.do_check()

def check_svnconflict_pro(queue_dict ,conf):
    '''检查是否存在冲突进程 '''
    print 'chenck_svnConflict_pro'
    try:
        print 'now check the mailbox:',queue_dict['svnconflict_queue'].get()
    except:
        print 'queue is empty'
    pass

def exec_script_pro(queue_dict ,conf):
    '''运行外部脚本进程'''
    print 'exec_script_pro'
    pass

def write_log_pro(queue_dict ,conf):
    '''公共写log的进程'''
    print 'write_log_pro'
    logger = Log.Log(queue_dict,conf)
    while True:
        try:
            log_msg = queue_dict['log_queue'].get()
            if log_msg :
                if log_msg == 'end':
                    break
                logger.log(log_msg)
            else:
                print 'log_queue get() timeout next'
                logger.log('get() timeout next')
        except:
            logger.log('read log queue error')

if __name__ == '__main__':
    notify_queue = Queue()  #--通知队列
    svnconflict_queue = Queue() #--冲突队列
    log_queue = Queue() #--log队列
    need_run_script_queue = Queue() #--脚本检查队列
    #--初始化所有队列
    queue_dict = {}
    for q_name in ['notify_queue','svnconflict_queue','log_queue','need_run_script_queue']:
        queue_dict.setdefault(q_name,Queue())

    #--配置信息
    cfg_file = 'config.ini'
    conf = AppConfig.AppConfig(cfg_file,'utf-8-sig')

    func_list = [svn_update_pro,notify_pro,check_pro,check_svnconflict_pro,exec_script_pro,write_log_pro]
    pro_list = []
    for func in func_list:
        print func,'start'
        func_name = func.func_name
        pro = Process(name = func_name,target = func,args = (queue_dict,conf))
        pro.daemon = True
        pro.start()
        pro_list.append(pro)

    for pro in pro_list:#--收割
        print pro,' ready to join'
        pro.join()
        print 'one joined'





